package gui;


import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

import localhost._8080.metaimage.Client;



public class EditionTab extends JPanel implements ActionListener, KeyListener{
	

	/**
	 * 
	 */
	private static final long serialVersionUID = -8182157861545790550L;

	private JLabel[] tabLab;
	private JTextField[] tabField;
	private JList j;
	
	private final static int NBFIELDS = 7;
	
	private JComboBox cb;
	
	private JLabel arcTypeLabel;
	private JComboBox arcType;
	
	
	JLabel previewLabel;
	private BufferedImage preview;
	private Graphics2D g;
	
	
	
	public EditionTab(JList j){
		super();
		this.j=j;
		
		//On rempli le type d'angle
		String[] arcTypeName = {"Open","Chord","Pie"};
		this.arcType = new JComboBox(arcTypeName);
		
		this.setLayout(new BorderLayout());
		
		Container pagestart = new Container();
		pagestart.setLayout(new FlowLayout(FlowLayout.CENTER));
		JLabel tips =new JLabel("<html><center><br>Type <strong>ALT</strong> to preview the shape<br>" +
				"Type <strong>Enter</strong> to add the shape<br><br>"	+
				"</center></html>");
		pagestart.add(tips);		
		tips.setBorder(BorderFactory.createTitledBorder("Tips" ));
		add(pagestart,BorderLayout.CENTER);
		
		
		
		String[] drawings ={"> Select a shape","createImage","addLine", "addRectangle","addArc","addEllipse","addPolygon"};
		cb = new JComboBox(drawings);
		cb.setBorder(BorderFactory.createTitledBorder("Shapes" ));
		Container c = new Container();
		c.setLayout(new FlowLayout(FlowLayout.CENTER));
		c.add(cb);
		add(c,BorderLayout.LINE_START);
		//cb.setPreferredSize(new Dimension(150, 30));
		
		
		cb.addActionListener(this);
		
		tabLab = new JLabel[NBFIELDS];
		tabField = new JTextField[NBFIELDS];
		
		Container cont = new Container();
		cont.setLayout(new FlowLayout(FlowLayout.CENTER));
		
		for (int i = 0;i< NBFIELDS;i++){
			tabLab[i] = new JLabel();
			tabLab[i].setVisible(false);
			
			tabField[i] = new JTextField(5);
			tabField[i].setVisible(false);
			tabField[i].addActionListener(this);
			
			cont.add(tabLab[i]);
			cont.add(tabField[i]);
		}
		
		this.arcTypeLabel = new JLabel("type : ");
		this.arcTypeLabel.setVisible(false);
		this.arcType.setVisible(false);
		cont.add(this.arcTypeLabel);
		cont.add(this.arcType);
		
		add(cont,BorderLayout.PAGE_END);
		
		this.previewLabel = new JLabel();
		this.preview = new BufferedImage(100, 100,  BufferedImage.TYPE_INT_RGB);
		g = preview.createGraphics();
		g.clearRect(0, 0, 100, 100);
		ImageIcon icon = new ImageIcon(preview);			
        previewLabel.setIcon(icon);
        if  (icon != null) {
        	previewLabel.setBorder(BorderFactory.createTitledBorder("Preview" ));
        	previewLabel.setIconTextGap(-165);
        }
        
        Container right = new Container();
        right.setLayout(new BoxLayout(right,BoxLayout.Y_AXIS));
        right.add(previewLabel);
		add(right,BorderLayout.LINE_END);
	
		
	}

	//Quand on selectionne un autre element de la liste on doit tout remettre a zero
	private void resetAllFields(){
		
		for (int i = 0;i< NBFIELDS;i++){
			
			tabLab[i].setVisible(false);	
			
			tabField[i].setVisible(false);
			tabField[i].setText("");
			tabField[i].addKeyListener(this);
		}
		this.arcTypeLabel.setVisible(false);
		this.arcType.setVisible(false);
		
		
	
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		
		if ((String) cb.getSelectedItem() == "addLine"){
			resetAllFields();
			
			//On affiche ensuite les elements necessaire a une ligne
			tabLab[0].setText("x1 : ");
			tabLab[0].setVisible(true);
			tabField[0].setVisible(true);
			
			tabLab[1].setText("y1 : ");
			tabLab[1].setVisible(true);
			tabField[1].setVisible(true);
			
			tabLab[2].setText("x2 : ");
			tabLab[2].setVisible(true);
			tabField[2].setVisible(true);
			
			tabLab[3].setText("y2 : ");
			tabLab[3].setVisible(true);
			tabField[3].setVisible(true);
			
			
		}
		else if((String) cb.getSelectedItem() == "addRectangle"){
			resetAllFields();
			
			tabLab[0].setText("x : ");
			tabLab[0].setVisible(true);
			tabField[0].setVisible(true);
			
			tabLab[1].setText("y : ");
			tabLab[1].setVisible(true);
			tabField[1].setVisible(true);
			
			tabLab[2].setText("width : ");
			tabLab[2].setVisible(true);
			tabField[2].setVisible(true);
			
			tabLab[3].setText("height : ");
			tabLab[3].setVisible(true);
			tabField[3].setVisible(true);
			
			
		}
		else if((String) cb.getSelectedItem() == "addArc"){
			resetAllFields();
			tabLab[0].setText("x : ");
			tabLab[0].setVisible(true);
			tabField[0].setVisible(true);
			
			tabLab[1].setText("y : ");
			tabLab[1].setVisible(true);
			tabField[1].setVisible(true);
			
			tabLab[2].setText("width : ");
			tabLab[2].setVisible(true);
			tabField[2].setVisible(true);
			
			tabLab[3].setText("height : ");
			tabLab[3].setVisible(true);
			tabField[3].setVisible(true);
			
			tabLab[4].setText("start angle : ");
			tabLab[4].setVisible(true);
			tabField[4].setVisible(true);
			
			tabLab[5].setText("extent : ");
			tabLab[5].setVisible(true);
			tabField[5].setVisible(true);
			
			/*tabLab[6].setText("type : ");
			tabLab[6].setVisible(true);
			tabField[6].setVisible(true);*/
			this.arcTypeLabel.setVisible(true);
			this.arcType.setVisible(true);
			
		}
		else if((String) cb.getSelectedItem() == "addEllipse"){
			resetAllFields();
			tabLab[0].setText("x : ");
			tabLab[0].setVisible(true);
			tabField[0].setVisible(true);
			
			tabLab[1].setText("y : ");
			tabLab[1].setVisible(true);
			tabField[1].setVisible(true);
			
			tabLab[2].setText("width : ");
			tabLab[2].setVisible(true);
			tabField[2].setVisible(true);
			
			tabLab[3].setText("height : ");
			tabLab[3].setVisible(true);
			tabField[3].setVisible(true);
			
		
		}
		else if((String) cb.getSelectedItem() == "addPolygon"){
		
			resetAllFields();
			tabLab[0].setText("x[ ] : ");
			tabLab[0].setVisible(true);
			tabField[0].setVisible(true);
			
			tabLab[1].setText("y[ ] : ");
			tabLab[1].setVisible(true);
			tabField[1].setVisible(true);
			
			tabLab[2].setText("Nb vertex : ");
			tabLab[2].setVisible(true);
			tabField[2].setVisible(true);
		}
		else if((String) cb.getSelectedItem() == "createImage"){
			
			resetAllFields();
			tabLab[0].setText("Image name : ");
			tabLab[0].setVisible(true);
			tabField[0].setVisible(true);
			
		}
		
		
	}

	@Override
	public void keyPressed(KeyEvent k) {
		if (k.getKeyCode() == KeyEvent.VK_ENTER){
			String a = new String("");
			
			if(((String)this.cb.getSelectedItem()).equalsIgnoreCase("createImage")){
				Client.execute((String)this.cb.getSelectedItem()+" "+tabField[0].getText());
				SplitPaneDemo.updateList();
				this.actionPerformed(null);
			}
			else{
				a= "";
				for (int i=0; i< NBFIELDS;i++){
					if(tabField[i].isVisible()){					
						a = a + tabField[i].getText() + " ";
					}
				}
				
				if (this.arcType.isVisible()){
					int val=Arc2D.OPEN;
					if (this.arcType.getSelectedItem() == "Open") val = Arc2D.OPEN;
					if (this.arcType.getSelectedItem() == "Chord") val = Arc2D.CHORD;
					if (this.arcType.getSelectedItem() == "Pie") val = Arc2D.PIE;				
					a += val;				
				}
				
				
				//System.out.println((String)this.cb.getSelectedItem()+" "+((String)j.getSelectedValue()).split(".xml")[0]+" "+ a);
				try{
					
					Client.execute((String)this.cb.getSelectedItem()+" "+((String)j.getSelectedValue()).split(".xml")[0]+" "+ a);
					SplitPaneDemo.updateLabel(((String)j.getSelectedValue()));
					this.actionPerformed(null);
				}
				catch(Exception e){
				}
			}
			
		}
		
		if (k.getKeyCode() == KeyEvent.VK_ALT){
			
			if ((String) cb.getSelectedItem() == "addLine"){
				
				g.clearRect(0, 0, 100, 100);
				g.draw(new Line2D.Double(Integer.parseInt(tabField[0].getText())/5
												,Integer.parseInt(tabField[1].getText())/5
												,Integer.parseInt(tabField[2].getText())/5,
												Integer.parseInt(tabField[3].getText())/5  ));
				
			}
			else if((String) cb.getSelectedItem() == "addRectangle"){
				g.clearRect(0, 0, 100, 100);
				g.draw(new Rectangle2D.Double(Integer.parseInt(tabField[0].getText())/5
												,Integer.parseInt(tabField[1].getText())/5
												,Integer.parseInt(tabField[2].getText())/5,
												Integer.parseInt(tabField[3].getText())/5 ));
				
			}
			else if((String) cb.getSelectedItem() == "addArc"){
				g.clearRect(0, 0, 100, 100);
				
				int val=Arc2D.OPEN;
				if (this.arcType.getSelectedItem() == "Open") val = Arc2D.OPEN;
				if (this.arcType.getSelectedItem() == "Chord") val = Arc2D.CHORD;
				if (this.arcType.getSelectedItem() == "Pie") val = Arc2D.PIE;
				
				
				g.draw(new Arc2D.Double(Integer.parseInt(tabField[0].getText())/5
												,Integer.parseInt(tabField[1].getText())/5
												,Integer.parseInt(tabField[2].getText())/5,
												Integer.parseInt(tabField[3].getText())/5,
												Integer.parseInt(tabField[4].getText()),
												Integer.parseInt(tabField[5].getText()),
												val));
				
			}
			else if((String) cb.getSelectedItem() == "addEllipse"){
				g.clearRect(0, 0, 100, 100);
				g.draw(new Ellipse2D.Double(Integer.parseInt(tabField[0].getText())/5
												,Integer.parseInt(tabField[1].getText())/5
												,Integer.parseInt(tabField[2].getText())/5,
												Integer.parseInt(tabField[3].getText())/5));
				
			
			}
			else if((String) cb.getSelectedItem() == "addPolygon"){
				g.clearRect(0, 0, 100, 100);
				String x_s[] = new String[Integer.parseInt(tabField[2].getText())];				
				x_s = tabField[0].getText().split(" ");
				
				String y_s[] = new String[Integer.parseInt(tabField[2].getText())];				
				y_s = tabField[1].getText().split(" ");
				
				
				int[] x = new int[Integer.parseInt(tabField[2].getText())];
				int[] y = new int[Integer.parseInt(tabField[2].getText())];
				
				for(int j=0;j<Integer.parseInt(tabField[2].getText()); j++){
					x[j] = Integer.parseInt(x_s[j]) /5;
					y[j] = Integer.parseInt(y_s[j]) /5;
					
				}
				
				g.draw(new Polygon(	x,
									y,
									Integer.parseInt(tabField[2].getText())));
			
			}
						
			ImageIcon icon = new ImageIcon(preview);			
	        previewLabel.setIcon(icon);
	        if  (icon != null) {
	        	previewLabel.setBorder(BorderFactory.createTitledBorder("Preview" ));
	        	previewLabel.setIconTextGap(-165);
	        }
	        previewLabel.repaint();
			
		}
		
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		
	}

	@Override
	public void keyTyped(KeyEvent k) {
		
		
		
	}
}
